﻿(function ($, document, undefined) {

	var pluses = /\+/g;

	function raw(s) {
		return s;
	}

	function decoded(s) {
		return unRfc2068(decodeURIComponent(s.replace(pluses, ' ')));
	}

	function unRfc2068(value) {
		if (value.indexOf('"') === 0) {
			// This is a quoted cookie as according to RFC2068, unescape
			value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
		}
		return value;
	}

	function fromJSON(value) {
		return config.json ? JSON.parse(value) : value;
	}

	var config = $.cookie = function (key, value, options) {

		// write
		if (value !== undefined) {
			options = $.extend({}, config.defaults, options);

			if (value === null) {
				options.expires = -1;
			}

			if (typeof options.expires === 'number') {
				var days = options.expires, t = options.expires = new Date();
				t.setDate(t.getDate() + days);
			}

			value = config.json ? JSON.stringify(value) : String(value);

			return (document.cookie = [
				encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
				options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
				options.path ? '; path=' + options.path : '',
				options.domain ? '; domain=' + options.domain : '',
				options.secure ? '; secure' : ''
			].join(''));
		}

		// read
		var decode = config.raw ? raw : decoded;
		var cookies = document.cookie.split('; ');
		var result = key ? null : {};
		for (var i = 0, l = cookies.length; i < l; i++) {
			var parts = cookies[i].split('=');
			var name = decode(parts.shift());
			var cookie = decode(parts.join('='));

			if (key && key === name) {
				result = fromJSON(cookie);
				break;
			}

			if (!key) {
				result[name] = fromJSON(cookie);
			}
		}

		return result;
	};

	config.defaults = {};

	$.removeCookie = function (key, options) {
		if ($.cookie(key) !== null) {
			$.cookie(key, null, options);
			return true;
		}
		return false;
	};

})(jQuery, document);

function item_mouseout(sender) {
	$(this).css('background-color', 'white');
}

function item_mouseover(sender) {
	$(this).css('background-color', '#F6F6F6');
}

function buildRangeDatePicker(start, end) {
	if (start && end) {
		$.datepicker.setDefaults($.datepicker.regional["zh-cn"]);
		$(start).datepicker({
			changeMonth: true,
			changeYear: true,
			onClose: function (selectedDate) {
				$(end).datepicker("option", "minDate", selectedDate);
			}
		});
		$(end).datepicker({
			changeMonth: true,
			changeYear: true,
			onClose: function (selectedDate) {
				$(start).datepicker("option", "maxDate", selectedDate);
			}
		});
	}
}

function anyitemchecked() {
	var chbItem = $(".chbitem input");

	var result = false;

	chbItem.each(function () {
		if (this.disabled != true) {
			if (this.checked == true) {
				result = true;
			}
		}
	});

	return result;
}

function showMessage(msg) {
	$('#alertmsg').find('.msgtext').html(msg);
	$('#alertmsg').show(100).delay(8000).hide(200);
}

function showError(err) {
	$('#errormsg').find('.msgtext').html(err);
	$('#errormsg').show(100).delay(8000).hide(200);
}

function btnExpand_click(sender, td, tablecontainer) {
	if ($(sender).hasClass('expand')) {
		if (tablecontainer) {
			$(tablecontainer).find('.detailrow').hide();
			$(tablecontainer).find('.collapse').attr('class', 'expand');
		}

		$(td).parent().show();
		$(sender).attr('class', 'collapse');
	}
	else if ($(sender).hasClass('collapse')) {
		$(td).parent().hide();
		$(sender).attr('class', 'expand');
	}

	return false;
}

function deleteConfirm(multirows) {
	if (multirows) {
		if (anyitemchecked()) {
			return confirm('确定要删除吗?');
		}
		else {
			return false;
		}
	}
	else {
		return confirm('确定要删除吗?');
	}
}

function ie6check() {
	$.browser.chrome = $.browser.webkit && !!window.chrome;

	if (!$.browser.chrome && $.browser.msie && $.browser.version == "6.0") {
		$('#ie6-warning').show();
	}
}

function getKPIOperateSvcURL() {
	var result = '';

	result += location.protocol + '//' + location.host;

	result += '/KPIOperateSvc.asmx/';

	return result;
}

function getMailSvcURL() {
	var result = '';

	result += location.protocol + '//' + location.host;

	result += '/MailSvc.asmx/';

	return result;
}

function groupcategorynode_click(sender) {
	if ($(sender).hasClass('collapsedgroup')) {
		$(sender).parent().find('.groupsincategory').hide();
		$(sender).removeClass('collapsedgroup');
	}
	else {
		$(sender).parent().find('.groupsincategory').show();
		$(sender).addClass('collapsedgroup');
	}
}

function logout_click() {
	$.removeCookie('KPI.UserIDCookie');
	window.close();
}

function disableCopy() {
	$(document).bind('keydown', function (evt) {
		var forbiddenKeys = new Array('a', 'n', 'c', 'x', 'v', 'j', 's');
		var key;
		var isCtrl = false;

		if (window.event) {
			key = window.event.keyCode;     //IE
			if (window.event.ctrlKey) {
				isCtrl = true;
			}
		}
		else {
			key = evt.which;     //firefox
			if (evt.ctrlKey) {
				isCtrl = true;
			}
		}

		if (key == 123) {
			evt.stopPropagation();
			evt.preventDefault();

			return false;
		}

		//if ctrl is pressed check if other key is in forbidenKeys array
		if (isCtrl) {
			for (i = 0; i < forbiddenKeys.length; i++) {
				//case-insensitive comparation
				if (forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase()) {
					evt.stopPropagation();
					evt.preventDefault();

					return false;
				}
			}
		}

		return true;
	});
}

function disableContextMenu() {
	$('body').attr('oncontextmenu', 'return false;');
}

function checkProgressContentMaxLength(ctl, args) {
	args.IsValid = (args.Value.length <= 300);
}

function checkKPIContentMaxLength(ctl, args) {
	args.IsValid = (args.Value.length <= 500);
}

function checkActionPlanMaxLength(ctl, args) {
	args.IsValid = (args.Value.length <= 500);
}

function checkEndOfTheYearAssessmentMaxLength(ctl, args) {
	args.IsValid = (args.Value.length <= 1000);
}

$(window).load(function () {
	ie6check();

	var h = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) - $('.header').height() - $('.footer').height() - $('.menu').height() - 22;

	$('.main').css('min-height', h);

	if ($('.menuitem')) {
		$('.menuitem').hover(function () {
			var submenucontainer = $(this).find('.submenucontainer');

			if (submenucontainer) {
				submenucontainer.show();
			}
		}, function () {
			var submenucontainer = $(this).find('.submenucontainer');

			if (submenucontainer) {
				submenucontainer.hide();
			}
		});
	}

	if ($('.menuitembox')) {
		$('.menuitembox').hover(function () {
			$(this).addClass('mouseon');

		}, function () {
			$(this).removeClass('mouseon');
		});
	}

	var chbAll = $(".chball input");
	var chbItem = $(".chbitem input");

	chbAll.bind("click", function () {
		chbItem.each(function () {
			if (this.disabled != true) {
				this.checked = chbAll[0].checked;
			}
		});
	});

	chbItem.bind("click", function () {
		if (this.checked == false) {
			chbAll[0].checked = false;
		}
	});

	$('.rowitem').hover(item_mouseover, item_mouseout);
});
